package com.jack.leetcode.math;

import java.util.HashMap;
import java.util.Map;

/***
 * @author zhangjie
 * @date 2020/5/9 20:32
 */
public class 整数转罗马数字 {

    public static void main(String[] args) {
        int num = 1994;
        System.out.println(intToRoman(num));
        num = 3;
        System.out.println(intToRoman(num));
        num = 4;
        System.out.println(intToRoman(num));
        num = 9;
        System.out.println(intToRoman(num));
        num = 58;
        System.out.println(intToRoman(num));
    }

    public static String intToRoman(int num) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "I");
        map.put(2, "II");
        map.put(3, "III");
        map.put(4, "IV");
        map.put(5, "V");
        map.put(6, "VI");
        map.put(7, "VII");
        map.put(8, "VIII");
        map.put(9, "IX");
        map.put(10, "X");
        map.put(20, "XX");
        map.put(30, "XXX");
        map.put(40, "XL");
        map.put(50, "L");
        map.put(90, "XC");
        map.put(100, "C");
        map.put(200, "CC");
        map.put(300, "CCC");
        map.put(400, "CD");
        map.put(500, "D");
        map.put(900, "CM");
        StringBuilder result = new StringBuilder();
        int divid = 1000;
        while (num - divid >= 0) {
            result.append("M");
            num = num - divid;
        }
        divid /= 10;
        while (divid > 0) {
            if (num - 9 * divid >= 0) {
                result.append(map.get(9 * divid));
                num = num - 9 * divid;
            } else if (num - 5 * divid >= 0) {
                result.append(map.get(5 * divid));
                num = num - 5 * divid;
            } else if (num - 4 * divid >= 0) {
                result.append(map.get(4 * divid));
                num = num - 4 * divid;
            } else if (num - 3 * divid >= 0) {
                result.append(map.get(3 * divid));
                num = num - 3 * divid;
            } else if (num - 2 * divid >= 0) {
                result.append(map.get(2 * divid));
                num = num - 2 * divid;
            } else if (num - divid >= 0) {
                result.append(map.get(divid));
                num = num - divid;
            }
            if(num / divid == 0){
                divid /= 10;
            }
        }
        return result.toString();
    }
}
